package com.dq.service.org.impl;

import com.dq.context.Constant;
import com.dq.controller.in.ServiceProjectQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.domain.base.impl.BaseDaoImpl;
import com.dq.exception.customException.AdminException;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.service.ServiceProject;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.org.ServiceProjectService;
import com.dq.service.service.ServiceProjectCategoryCentrService;
import com.dq.service.sys.user.SysUserService;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author cf
 *
 */
@Service
public class ServiceProjectServiceImpl extends BaseRepositories implements ServiceProjectService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ServiceProjectCategoryCentrService serviceProjectCategoryCentrService;

    /**
     * 添加
     * @param serviceProject
     * @return
     * @throws AdminException
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ServiceProject save(ServiceProject serviceProject)  throws AdminException {
        serviceProject.setVersion(Constant.VERSION_VALUE);
        serviceProject.setDeleted(false);
        serviceProject.setCreateTime(System.currentTimeMillis());
        serviceProject.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        serviceProject.setCreateUser(user);
        serviceProject.setUpdateUser(user);
        /// 编号
        serviceProject.setProjectNo(OrgPosition.generyDatetimeAndRandomNum(Constant.ORDERS_SUFF_COUNT));
        ServiceProject flush = serviceProjectRepository.saveAndFlush(serviceProject);

        // 保存服务项目-类别关联
        serviceProjectCategoryCentrService.relation(serviceProject.getServiceCategoryIds(), flush);


        return flush;
    }

    /**
     * 修改, 不需要修改 关联类别， setServiceCategoryIds().size = 0
     * @param serviceProject
     * @return
     * @throws AdminException
     */
    @Override
    public ServiceProject update(ServiceProject serviceProject) throws AdminException {
        serviceProject.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        if(user != null) {
            serviceProject.setUpdateUser(user);
        }

        ServiceProject flush = serviceProjectRepository.saveAndFlush(serviceProject);

        if(serviceProject.getServiceCategoryIds() != null && serviceProject.getServiceCategoryIds().size() > 0) {
            // 保存服务项目-类别关联
            serviceProjectCategoryCentrService.relation(serviceProject.getServiceCategoryIds(), flush);
        }

        return flush;
    }

    @Override
    public List<ServiceProject> list(ServiceProjectQuery serviceProjectQuery) throws AdminException {
        Map<String, Object> query = createQuery(serviceProjectQuery);

        return dao.find(dao.query2Hql(ServiceProject.class, query), query);
    }
    @Override
    public Long count(ServiceProjectQuery serviceProjectQuery) throws AdminException {
        Map<String, Object> query = createQuery(serviceProjectQuery);

        return dao.count(dao.count2Hql(ServiceProject.class, query), query);
    }

    private Map<String, Object> createQuery(ServiceProjectQuery serviceProjectQuery) {
        Map<String, Object> query = serviceProjectQuery.parseMap();

        if (StringUtils.isNotBlank(serviceProjectQuery.getProjectName())) {
            query.put("projectNameLike", BaseAdminQuery.likeSql(serviceProjectQuery.getProjectName()));
            query.remove("projectName");
        }
        if(StringUtils.isNotEmpty(serviceProjectQuery.getProjectNo())){
            query.put("projectNo"+ BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(serviceProjectQuery.getProjectNo()));
            query.remove("projectNo");
        }
        if(serviceProjectQuery.getBeginTime() != null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,serviceProjectQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(serviceProjectQuery.getEndTime() != null){
            query.put("createTime"+BaseDaoImpl.lteSqlStr,serviceProjectQuery.getEndTime());
            query.remove("endTime");
        }
        return query;
    }
}

